package com.lzjt.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzjt.entity.AccountEntity;
import com.lzjt.entity.DoctorEntity;
import com.lzjt.mapper.AccountMapper;
import com.lzjt.model.DoctorLevelModel;
import com.lzjt.model.DoctorModel;
import com.lzjt.model.TreaTypeModel;
import com.lzjt.param.DoctorParam;
import com.lzjt.util.Msg;

import java.util.List;
import com.lzjt.mapper.DoctorMapper; // 假设 DoctorMapper 存在于 mapper 包下

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; // 注入相关
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service // 标记为 Spring Service Bean
public class DoctorService {

    @Autowired
    private DoctorMapper doctorMapper; // 注入 Mapper
    // 在类中添加 AccountMapper 的注入
    private AccountMapper accountMapper; // 添加这一行代码


    public Msg getDoctorWithPage(DoctorParam param) {
        if (param.getSize() == 0) {
            param.setSize(1);
        }
        PageHelper.startPage(param.getPn(), param.getSize());
        List<DoctorModel> list = doctorMapper.getAllDoctor(param);
        PageInfo<DoctorModel> info = new PageInfo<>(list, 5);
        return Msg.success().data("doctorInfo", info); // 使用 addData 避免类型冲突
    }

    public Msg getLevelAndType() {
        List<TreaTypeModel> allTreatType = doctorMapper.getAllTreatType();
        List<DoctorLevelModel> allLevel = doctorMapper.getAllLevel();

        return Msg.success()
                .data("allTreatType", allTreatType)
                .data("allLevel", allLevel);
    }
    public Msg saveDoctor(DoctorParam param) {
        int i,j;
        AccountEntity aEntity = new AccountEntity();
        aEntity.setPhoneNumber(param.getPhoneNumber());
        aEntity.setUname(param.getName()+param.getPhoneNumber().substring(7));//医生用户名姓名+手机号后四位
        aEntity.setRealname(param.getName());
        aEntity.setPwd(new BCryptPasswordEncoder().encode(param.getPwd()));
        // 第一处：保存医生信息时设置创建时间
        aEntity.setCreatetime(DateUtil.date()); // 使用 Hutool 的 DateUtil 获取 Date 实例

        // 第二处：设置更新时间
        aEntity.setUpdatetime(DateUtil.date());

        aEntity.setUtype("ROLE_2");
        int checkPhone = accountMapper.checkPhone(param.getPhoneNumber());
        if (checkPhone > 0) {
            return Msg.fail().code(10001).mess("手机号已被使用");
        }
        try {
            i = accountMapper.regist(aEntity);
        } catch (DuplicateKeyException e) {
            return Msg.fail().mess("该账号已经注册");
        }
        DoctorEntity de = new DoctorEntity();
        BeanUtils.copyProperties(param,de);// 第一处：保存医生信息时设置创建时间
        de.setCreateTime(DateUtil.date()); // 使用 Hutool 的 DateUtil 获取 Date 实例

        // 第二处：设置更新时间
        de.setUpdateTime(DateUtil.date());

        de.setAccountId(aEntity.getId());
        j = doctorMapper.saveDoctor(de);
        if (i > 0 && j > 0) {
            List<DoctorModel> allDoctor = doctorMapper.getAllDoctor(null);
            de.setTotal((long) allDoctor.size());
            param.setPwd("");
            Long num = de.getTotal() % 5 == 0 ? (de.getTotal() / 5) : (de.getTotal() / 5)+1;
            return Msg.success().mess("添加成功").data("pages",num).data("addData",param);
        }
        return Msg.fail().mess("添加失败");
    }
    public Msg updateDoctor(Long id, DoctorParam param) {
        int checkPhone = accountMapper.checkPhone(param.getPhoneNumber());
        if (checkPhone > 0) {
            return Msg.fail().code(10001).mess("手机号已被使用");
        }
        AccountEntity ae = new AccountEntity();
        ae.setId(param.getAccountId());
        System.out.println(param.getAccountId());
        ae.setUpdatetime(DateUtil.date()); // 推荐方式，保持与类中其他代码风格一致

        ae.setUname(param.getName()+param.getPhoneNumber().substring(7));
        ae.setPhoneNumber(param.getPhoneNumber());
        int j = accountMapper.updateAccount(ae);//
        DoctorEntity de = new DoctorEntity();
        BeanUtils.copyProperties(param, de);
        de.setId(id);
        de.setUpdateTime(DateUtil.date());

        int i = doctorMapper.updateDoctor(de);
        if (i > 0 && j > 0) {
            param.setPwd("");
            return Msg.success().mess("修改成功").data("updateData", param);
        }
        return Msg.fail().mess("修改失败");
    }
    public Msg deleteDoctorById(Long id) {
        int i = doctorMapper.deleteDoctorById(id);
        if (i >= 2) {
            return Msg.success().mess("删除成功").data("deleteDocInfo",1).data("deleteDocAccount",1);
        }
        return Msg.fail().mess("删除失败");
    }

public Msg resetPwd(Long id) {
    String newPwd = new BCryptPasswordEncoder().encode("666666");
    int i = accountMapper.resetPwd(id, newPwd);// 使用注入的 accountMapper 实例
    if (i > 0) {
        return Msg.success().mess("重置成功");
    }
    return Msg.fail().mess("重置失败");
}

}
